AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
include('modules/dialling.lua')

function ENT:SpawnFunction(p,t)
	if (not t.Hit) then return end
	local e = ents.Create("stargate_interserver");
	e:SetPos(t.HitPos+Vector(0,0,90));
	e:Spawn();
	e:Activate();
	--################# Set correct angle for the spawned prop
	local ang = p:GetAimVector():Angle(); ang.p = 0; ang.r = 0; ang.y = (ang.y+180) % 360
	e:SetAngles(ang);
	e:SetGateGroup("P@");
	e:SetLocale(true);
	e:CartersRamps(t);
	e:SetWire("Dialing Mode",-1);
	return e;
end

function ENT:Initialize()
	util.PrecacheModel(self.Models.Base);
	util.PrecacheModel(self.Models.Chevron);
	util.PrecacheModel(self.Models.Dial[1]);
	util.PrecacheModel(self.Models.Incoming[1]);
	util.PrecacheModel(self.Models.Dial[2]);
	util.PrecacheModel(self.Models.Incoming[2]);

	self.Entity:SetModel(self.Models.Base);
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.Entity:Fire("skin",1);
	self.BaseClass.BaseClass.Initialize(self); -- BaseClass Initialize call
	self:AddRing();
	self:AddChevron();
	self.SpinLight = 1;
	
	self.RemoteIp = "87.117.217.25:27015"
	self.Opened = false
end

--################# Openes the gate (creates the event horizon) @aVoN
function ENT:Open()
	local e = ents.Create("event_horizon_interserver");

	e:SetPos(self.Entity:GetPos());
	e:SetAngles(self.Entity:GetAngles());
	e:SetParent(self.Entity);
	-- Set another sound to the gate (must be done before spawning it)
	if(self.Sounds and self.Sounds.Open) then
		e.Sounds.Open = self.Sounds.Open;
	end
	e:Spawn();
	e:Activate();
	if(self.Outbound) then e:SetTarget(self.Target) end;  -- We tell the Eventhorizon the GATE (not the EH of the gate) where to find the EH of this gate
	if(IsValid(self.EventHorizon)) then self.EventHorizon:Remove() end; -- WORKAROUND for the EH-Stay bug: New EH's will definitely override the old!
	self.EventHorizon = e;
	self.OnButtLock = false;
	
	self.Opened = true
end

function ENT:ActivateStargate(inbound,fast)

	local action = self.Sequence:New();
	self:DeactivateStargate();
	action = self.Sequence:Dial(0,0,0,0) + action;
	self.Outbound = true;
	self:RunActions(action);

end

function ENT:Use(p)
	
	if(self.Opened == true) then
		self:Close();
	else
		self:ActivateStargate(0,0);
	end

end

function ENT:Close(ignore)
	self:StopActions();
	-- Remove the EH
	if(self.EventHorizon and self.EventHorizon:IsValid()) then
		self.EventHorizon:Shutdown(ignore);
	end
	-- Stop all chevrons
	local action = self.Sequence:New({
		{f=self.SetStatus,v={self,true,true},d=0},
		{pause=true,d=2.7},
	});
	action:Add({f=self.SetShutdown,v={self,true},d=0});
	for i=1,9 do
		action:Add({f=self.ActivateChevron,v={self,i,false},d=0});
	end
	action:Add({f=self.SetShutdown,v={self,true},d=0});
	action:Add({f=self.RingLight,v={self,0,true},d=0});

	self:RunActions(action);
	self.Opened = false;

end